/* GMGPrintCartWishlistManager.js <?php
#   --------------------------------------------------------------
#   GMGPrintCartWishlistManager.js 2011-05-18 gambio
#   Gambio GmbH
#   http://www.gambio.de
#   Copyright (c) 2011 Gambio GmbH
#   Released under the GNU General Public License (Version 2)
#   [http://www.gnu.org/licenses/gpl-2.0.html]
#   --------------------------------------------------------------
?>*/
/*<?php
if($GLOBALS['coo_debugger']->is_enabled('uncompressed_js') == false)
{
?>*/
function GMGPrintCartWishlistManager(){this.get_customers_data=function(p_coo_surfaces_manager,p_target){var t_found_file=false;var count_passes=0;var count_uploads=0;var coo_cart_wishlist_manager=this;$('#cart_quantity input').each(function(){if($(this).attr('type')=='file'){count_uploads++;}});if($('#details_cart_part').length>0){$('#details_cart_part').hide();$('#details_cart_part').after('<img src="gm/images/gprint/upload.gif" width="16" height="11" class="gm_gprint_loading" />');}$('#cart_quantity input').each(function(){if($(this).attr('type')=='file'){t_found_file=true;t_success=true;var t_upload_field_id=$(this).attr('id');var t_element_container=t_upload_field_id.replace(/_/g,'_container_');var t_products_id=coo_cart_wishlist_manager.get_products_id();var t_properties_values_ids=coo_cart_wishlist_manager.get_properties_values_ids();$('#'+t_upload_field_id).hide();$('#'+t_element_container).append('<img src="gm/images/gprint/upload.gif" width="16" height="11" class="gm_gprint_loading" id="loading_'+t_upload_field_id+'" />');$.ajaxFileUpload({url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php?action=upload&upload_field_id='+encodeURIComponent(t_upload_field_id)+'&products_id='+encodeURIComponent(t_products_id)+'&target='+encodeURIComponent(p_target)+'&mode='+c_mode+t_properties_values_ids+'&XTCsid='+gm_session_id,secureuri:false,fileElementId:t_upload_field_id,dataType:'json',success:function(p_filename){if(p_filename['ERROR']==false){$('#loading_'+p_filename['UPLOAD_FIELD_ID']).remove();count_passes++;if(count_passes==count_uploads&&t_success==true){$('#'+t_element_container).html('');if(p_target=='cart'){coo_cart_wishlist_manager.add_cart(p_coo_surfaces_manager);}else if(p_target=='wishlist'){coo_cart_wishlist_manager.add_wishlist(p_coo_surfaces_manager);}}else if(t_success==false){$('#'+p_filename['UPLOAD_FIELD_ID']).show();}}else{$('.gm_gprint_loading').remove();if($('#details_cart_part').length>0){$('#details_cart_part').show();}$('.gm_gprint_surface input').each(function(){if($(this).attr('type')=='file'){$(this).show();}});t_success=false;alert(gm_unescape(p_filename['ERROR_MESSAGE']));}},error:function(){if(fb)console.log("Upload failed: "+t_upload_field_id);}});}});if(!t_found_file){if(p_target=='cart'){coo_cart_wishlist_manager.add_cart(p_coo_surfaces_manager);}else if(p_target=='wishlist'){coo_cart_wishlist_manager.add_wishlist(p_coo_surfaces_manager);}}};this.add_cart=function(p_coo_surfaces_manager){var t_user_input='';var coo_surface=null;for(t_surfaces_id in p_coo_surfaces_manager.v_surfaces){coo_surface=p_coo_surfaces_manager.v_surfaces[t_surfaces_id];for(t_elements_id in coo_surface.v_elements){if(coo_surface.v_elements[t_elements_id].get_type()=='text_input'||coo_surface.v_elements[t_elements_id].get_type()=='textarea'||coo_surface.v_elements[t_elements_id].get_type()=='dropdown'){t_user_input+='&element_'+encodeURIComponent(t_elements_id)+'='+encodeURIComponent($('#element_'+t_elements_id).val());}}}var t_products_id=this.get_products_id();t_products_id=encodeURIComponent(t_products_id);var t_properties_values_ids=this.get_properties_values_ids();var t_success=jQuery.ajax({data:'action=add_cart'+t_user_input+'&products_id='+t_products_id+'&mode='+c_mode+t_properties_values_ids+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).responseText;if(t_success=='true'){document.cart_quantity.submit();}};this.update_cart=function(){var t_success=false;var t_delete=false;$('#cart_quantity input').each(function(){if($(this).attr('type')=='checkbox'&&$(this).attr('checked')==true){t_delete=true;var t_products_id=encodeURIComponent($(this).val());t_success=jQuery.ajax({data:'action=update_cart&products_id='+t_products_id+'&mode='+c_mode+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).json;}});if(!t_delete){t_success=true;}return t_success;};this.add_wishlist=function(p_coo_surfaces_manager){var t_user_input='';var coo_surface=null;for(t_surfaces_id in p_coo_surfaces_manager.v_surfaces){coo_surface=p_coo_surfaces_manager.v_surfaces[t_surfaces_id];for(t_elements_id in coo_surface.v_elements){if(coo_surface.v_elements[t_elements_id].get_type()=='text_input'||coo_surface.v_elements[t_elements_id].get_type()=='textarea'||coo_surface.v_elements[t_elements_id].get_type()=='dropdown'){t_user_input+='&element_'+t_elements_id+'='+encodeURIComponent($('#element_'+t_elements_id).val());}}}var t_products_id=this.get_products_id();t_products_id=encodeURIComponent(t_products_id);var t_properties_values_ids=this.get_properties_values_ids();var t_success=jQuery.ajax({data:'action=add_wishlist'+t_user_input+'&products_id='+t_products_id+'&mode='+c_mode+t_properties_values_ids+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).responseText;if(t_success=='true'){submit_to_wishlist();}};this.update_wishlist=function(){var t_success=false;$('.wishlist_checkbox').each(function(){if($(this).attr('type')=='checkbox'&&$(this).attr('checked')==true){var t_products_id=encodeURIComponent($(this).val());t_success=jQuery.ajax({data:'action=update_wishlist&products_id='+t_products_id+'&mode='+c_mode+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).json;}});return t_success;};this.wishlist_to_cart=function(){var t_success=false;$('.wishlist_checkbox').each(function(){if($(this).attr('type')=='checkbox'&&$(this).attr('checked')==true){var t_products_id=encodeURIComponent($(this).val());t_success=jQuery.ajax({data:'action=wishlist_to_cart&products_id='+t_products_id+'&mode='+c_mode+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).json;}});return t_success;};this.get_products_id=function(){var t_option_id='0';var t_option_value_id='0';var t_ids=$('#gm_products_id').val();$('.gm_attr_calc_input').each(function(){if((($(this).attr('checked')==true&&$(this).attr('type')=='radio')||$(this).attr('type')!='radio')&&$(this).attr('name').search('properties_values_ids')==-1){t_option_id=$(this).attr('name');t_option_id=t_option_id.replace(/id\[/g,'{');t_option_id=t_option_id.replace(/\]/g,'}');t_option_value_id=$(this).val();t_ids+=t_option_id+t_option_value_id;}});t_option_id=$('#gm_gprint_random').attr('name');t_option_id=t_option_id.replace(/id\[/g,'{');t_option_id=t_option_id.replace(/\]/g,'}');t_ids+=t_option_id+$('#gm_gprint_random').val();return t_ids;};this.get_properties_values_ids=function(){var t_properties_values_ids='';$('select[name="properties_values_ids[]"]').each(function(){t_properties_values_ids+='&'+encodeURIComponent('properties_values_ids[]')+'='+$(this).val();});return t_properties_values_ids;};this.copy_file=function(p_elements_id,p_old_product,p_new_product,p_target,p_source){var c_elements_id=gm_gprint_clear_number(p_elements_id);var c_old_product=encodeURIComponent(p_old_product);var c_new_product=encodeURIComponent(p_new_product);var c_target=encodeURIComponent(p_target);var c_source=encodeURIComponent(p_source);if(p_target=='cart'){t_success=jQuery.ajax({data:'action=copy_file&elements_id='+c_elements_id+'&old_product='+c_old_product+'&new_product='+c_new_product+'&target='+c_target+'&source='+c_source+'&mode='+c_mode+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).json;}else if(p_target=='wishlist'){t_success=jQuery.ajax({data:'action=copy_file&elements_id='+c_elements_id+'&old_product='+c_old_product+'&new_product='+c_new_product+'&target='+c_target+'&source='+c_source+'&mode='+c_mode+'&XTCsid='+gm_session_id,url:'<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',type:"POST",async:false}).json;}};}
/*<?php
}
else
{
?>*/
function GMGPrintCartWishlistManager()
{
	this.get_customers_data = function(p_coo_surfaces_manager, p_target)
	{
		var t_found_file = false;
		var count_passes = 0;
		var count_uploads = 0;
		var coo_cart_wishlist_manager = this;

		$('#cart_quantity input').each(function()
		{
			if($(this).attr('type') == 'file')
			{
				count_uploads++;
			}
		});

		if($('#details_cart_part').length > 0)
		{
			$('#details_cart_part').hide();
			$('#details_cart_part').after('<img src="gm/images/gprint/upload.gif" width="16" height="11" class="gm_gprint_loading" />')
		}

		$('#cart_quantity input').each(function()
		{
			if($(this).attr('type') == 'file')
			{
				t_found_file = true;
				t_success = true;
				var t_upload_field_id = $(this).attr('id');
				var t_element_container = t_upload_field_id.replace(/_/g, '_container_');
				var t_products_id = coo_cart_wishlist_manager.get_products_id();
				var t_properties_values_ids = coo_cart_wishlist_manager.get_properties_values_ids();

				$('#' + t_upload_field_id).hide();
				$('#' + t_element_container).append('<img src="gm/images/gprint/upload.gif" width="16" height="11" class="gm_gprint_loading" id="loading_' + t_upload_field_id + '" />');

				$.ajaxFileUpload({
					url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php?action=upload&upload_field_id=' + encodeURIComponent(t_upload_field_id)
							+ '&products_id=' + encodeURIComponent(t_products_id)
							+ '&target=' + encodeURIComponent(p_target)
							+ '&mode=' + c_mode
							+ t_properties_values_ids
							+ '&XTCsid=' + gm_session_id,
					secureuri: false,
					fileElementId: t_upload_field_id,
					dataType: 'json',
					success: function(p_filename)
					{
						if(p_filename['ERROR'] == false)
						{
							$('#loading_' + p_filename['UPLOAD_FIELD_ID']).remove();

							count_passes++;
							if(count_passes == count_uploads && t_success == true)
							{
								$('#'+t_element_container).html('');

								if(p_target == 'cart')
								{
									coo_cart_wishlist_manager.add_cart(p_coo_surfaces_manager);
								}
								else if(p_target == 'wishlist')
								{
									coo_cart_wishlist_manager.add_wishlist(p_coo_surfaces_manager);
								}
							}
							else if(t_success == false)
							{
								$('#' + p_filename['UPLOAD_FIELD_ID']).show();
							}
						}
						else
						{
							$('.gm_gprint_loading').remove();

							if($('#details_cart_part').length > 0)
							{
								$('#details_cart_part').show();

							}

							$('.gm_gprint_surface input').each(function()
							{
								if($(this).attr('type') == 'file')
								{
									$(this).show();
								}
							});

							t_success = false;

							alert(gm_unescape(p_filename['ERROR_MESSAGE']));
						}
					},
					error: function()
					{
						if(fb)console.log("Upload failed: " + t_upload_field_id);
					}
				});

			}
		});

		if(!t_found_file)
		{
			if(p_target == 'cart')
			{
				coo_cart_wishlist_manager.add_cart(p_coo_surfaces_manager);
			}
			else if(p_target == 'wishlist')
			{
				coo_cart_wishlist_manager.add_wishlist(p_coo_surfaces_manager);
			}
		}
	}

	this.add_cart = function(p_coo_surfaces_manager)
	{
		var t_user_input = '';
		var coo_surface = null;

		for(t_surfaces_id in p_coo_surfaces_manager.v_surfaces)
		{
			coo_surface = p_coo_surfaces_manager.v_surfaces[t_surfaces_id];

			for(t_elements_id in coo_surface.v_elements)
			{
				if(coo_surface.v_elements[t_elements_id].get_type() == 'text_input'
					|| coo_surface.v_elements[t_elements_id].get_type() == 'textarea'
					|| coo_surface.v_elements[t_elements_id].get_type() == 'dropdown')
				{
					t_user_input += '&element_' + encodeURIComponent(t_elements_id) + '=' + encodeURIComponent($('#element_' + t_elements_id).val());
				}
			}
		}

		var t_products_id = this.get_products_id();		
		t_products_id = encodeURIComponent(t_products_id);
		var t_properties_values_ids = this.get_properties_values_ids();
		
		var t_success = jQuery.ajax({
            data: 'action=add_cart' + t_user_input + '&products_id=' + t_products_id + '&mode=' + c_mode + t_properties_values_ids + '&XTCsid=' + gm_session_id,
            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
            type: "POST",
            async: false
        }).responseText;

		if(t_success == 'true')
		{
			document.cart_quantity.submit();
		}
	}

	this.update_cart = function()
	{
		var t_success = false;
		var t_delete = false;

		$('#cart_quantity input').each(function()
		{
			if($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true)
			{
				t_delete = true;
				var t_products_id = encodeURIComponent($(this).val());

				t_success = jQuery.ajax({
		            data: 'action=update_cart&products_id=' + t_products_id + '&mode=' + c_mode + '&XTCsid=' + gm_session_id,
		            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
		            type: "POST",
		            async: false
		        }).json;
			}
		});

		if(!t_delete)
		{
			t_success = true;
		}

		return t_success;
	}

	this.add_wishlist = function(p_coo_surfaces_manager)
	{
		var t_user_input = '';
		var coo_surface = null;

		for(t_surfaces_id in p_coo_surfaces_manager.v_surfaces)
		{
			coo_surface = p_coo_surfaces_manager.v_surfaces[t_surfaces_id];

			for(t_elements_id in coo_surface.v_elements)
			{
				if(coo_surface.v_elements[t_elements_id].get_type() == 'text_input'
					|| coo_surface.v_elements[t_elements_id].get_type() == 'textarea'
					|| coo_surface.v_elements[t_elements_id].get_type() == 'dropdown')
				{
					t_user_input += '&element_' + t_elements_id + '=' + encodeURIComponent($('#element_' + t_elements_id).val());
				}
			}
		}

		var t_products_id = this.get_products_id();
		t_products_id = encodeURIComponent(t_products_id);
		var t_properties_values_ids = this.get_properties_values_ids();

		var t_success = jQuery.ajax({
            data: 'action=add_wishlist' + t_user_input + '&products_id=' + t_products_id + '&mode=' + c_mode + t_properties_values_ids + '&XTCsid=' + gm_session_id,
            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
            type: "POST",
            async: false
        }).responseText;

		if(t_success == 'true')
		{
			submit_to_wishlist();
		}
	}

	this.update_wishlist = function()
	{
		var t_success = false;

		$('.wishlist_checkbox').each(function()
		{
			if($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true)
			{
				var t_products_id = encodeURIComponent($(this).val());

				t_success = jQuery.ajax({
		            data: 'action=update_wishlist&products_id=' + t_products_id + '&mode=' + c_mode + '&XTCsid=' + gm_session_id,
		            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
		            type: "POST",
		            async: false
		        }).json;
			}
		});

		return t_success;
	}

	this.wishlist_to_cart = function()
	{
		var t_success = false;

		$('.wishlist_checkbox').each(function()
		{
			if($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true)
			{
				var t_products_id = encodeURIComponent($(this).val());

				t_success = jQuery.ajax({
							data: 'action=wishlist_to_cart&products_id=' + t_products_id + '&mode=' + c_mode + '&XTCsid=' + gm_session_id,
				            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
				            type: "POST",
				            async: false
							}).json;
			}
		});

		return t_success;
	}

	this.get_products_id = function()
	{
		var t_option_id = '0';
		var t_option_value_id = '0';
		var t_ids = $('#gm_products_id').val();

		$('.gm_attr_calc_input').each(function()
		{
			if((($(this).attr('checked') == true && $(this).attr('type') == 'radio') || $(this).attr('type') != 'radio') && $(this).attr('name').search('properties_values_ids') == -1)
			{
				t_option_id = $(this).attr('name');
				t_option_id = t_option_id.replace(/id\[/g, '{');
				t_option_id = t_option_id.replace(/\]/g, '}');
				t_option_value_id = $(this).val();

				t_ids += t_option_id + t_option_value_id;
			}

		});

		t_option_id = $('#gm_gprint_random').attr('name');
		t_option_id = t_option_id.replace(/id\[/g, '{');
		t_option_id = t_option_id.replace(/\]/g, '}');

		t_ids += t_option_id + $('#gm_gprint_random').val();

		return t_ids;
	}

	this.get_properties_values_ids = function()
	{
		var t_properties_values_ids = '';

		$('select[name="properties_values_ids[]"]').each(function()
		{
			t_properties_values_ids += '&' + encodeURIComponent('properties_values_ids[]') + '=' + $(this).val();
		});

		return t_properties_values_ids;
	}

	this.copy_file = function(p_elements_id, p_old_product, p_new_product, p_target, p_source)
	{
		var c_elements_id = gm_gprint_clear_number(p_elements_id);
		var c_old_product = encodeURIComponent(p_old_product);
		var c_new_product = encodeURIComponent(p_new_product);
		var c_target = encodeURIComponent(p_target);
		var c_source = encodeURIComponent(p_source);

		if(p_target == 'cart')
		{
			t_success = jQuery.ajax({
				data: 'action=copy_file&elements_id=' + c_elements_id + '&old_product=' + c_old_product + '&new_product=' + c_new_product + '&target=' + c_target + '&source=' + c_source + '&mode=' + c_mode + '&XTCsid=' + gm_session_id,
	            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
	            type: "POST",
	            async: false
				}).json;
		}
		else if(p_target == 'wishlist')
		{
			t_success = jQuery.ajax({
				data: 'action=copy_file&elements_id=' + c_elements_id + '&old_product=' + c_old_product + '&new_product=' + c_new_product + '&target=' + c_target + '&source=' + c_source + '&mode=' + c_mode + '&XTCsid=' + gm_session_id,
	            url: '<?php echo GM_HTTP_SERVER . DIR_WS_CATALOG; ?>gm_gprint_ajax.php',
	            type: "POST",
	            async: false
				}).json;
		}
	}
}
/*<?php
}
?>*/
